﻿package idv.cjcat.emitter {
	use namespace engine;
	
	import idv.cjcat.emitter.geom.*;
	
	//phase 1
	public class LineSource extends ParticleSource {
		
		public var length:Number;
		public var normal:Vector2D = new Vector2D(0, -1);
		/**
		 * The higher the center-bias iteration, the more particles are concentrated to the center of the source.
		 */
		public var centerBiasIterations:uint = 0;
		
		/**
		 * Line-shaped particle source.
		 * @param	length
		 */
		public function LineSource(length:Number = 100) {
			this.length = length;
			
		}
		
		override public function spawnPos():MotionData2D {
			var temp:MotionData2D = new MotionData2D();
			var slopeAng:Number = Math.atan2(normal.y, normal.x) + Math.PI * 0.5;
			var randPos:Number = EmitterMath.normalDistribution(0.5, 0.5, centerBiasIterations + 1);
			temp.x = x + EmitterMath.interpolate(0, -length * 0.5 * Math.cos(slopeAng), 1, length * 0.5 * Math.cos(slopeAng), randPos);
			temp.y = y + EmitterMath.interpolate(0, -length * 0.5 * Math.sin(slopeAng), 1, length * 0.5 * Math.sin(slopeAng), randPos);
			return temp;
		}
		
		
		//XML
		//------------------------------------------------------------------------------------------------
		
		override public function toXML():XML {
			var xml:XML = super.toXML();
			xml.@type = "line";
			xml.@length = length.toString();
			xml.@normalX = normal.x.toString();
			xml.@normalY = normal.y.toString();
			xml.@centerBiasIterations = centerBiasIterations.toString();
			
			return xml;
		}
		override public function parseXML(xml:XML):void {
			super.parseXML(xml);
			
			length = parseFloat(xml.@length);
			normal.x = parseFloat(xml.@normalX);
			normal.y = parseFloat(xml.@normalY);
			centerBiasIterations = parseInt(xml.@centerBiasIterations);
		}
		
		//------------------------------------------------------------------------------------------------
	}
}